package com.exit490.cfp.activities.transacao.tab;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import org.w3c.dom.ls.LSInput;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.exit490.cfp.R;
import com.exit490.cfp.adapters.TransacaoAdapter;
import com.exit490.cfp.dao.TransacaoDAO;
import com.exit490.cfp.helper.DateHelpper;
import com.exit490.cfp.modelo.Transacao;

public class ReceitaTab extends Fragment {

    private Button botaoMesAnterior;
    private Button botaoMesPosterior;
    private TextView textviewMes;
    private View view;
    private Date dateNow;
    private ListView listViewTranscao;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.tab_receitas, container, false);
        buscaComponentes();
        return view;
    }

    public void buscaComponentes() {

        textviewMes = (TextView) view.findViewById(R.id.textview_mes);
        botaoMesAnterior = (Button) view.findViewById(R.id.botao_mes_anterior);
        botaoMesPosterior = (Button) view.findViewById(R.id.botao_mes_posterior);
        defineCliqueBotaoMesAnterior();
        defineCliqueBotaoMesPosterior();
        defineMes();
        populaListViewTranscao();

    }

    public void populaListViewTranscao() {
        listViewTranscao = (ListView) view.findViewById(R.id.listview_transacao);
        TransacaoDAO transacaoDAO = new TransacaoDAO(getActivity());
        ArrayList<Transacao> listaTransacao = transacaoDAO.selecionarTodasTranscoes();
        TransacaoAdapter transcaoAdapter = new TransacaoAdapter(getActivity(), R.layout.adapter_transcao, listaTransacao);
        listViewTranscao.setAdapter(transcaoAdapter);
    }

    public void defineMes() {

        Calendar c = Calendar.getInstance();
        dateNow = c.getTime();
        textviewMes.setText(DateHelpper.retornaNomeDoMes(dateNow));
    }

    public void defineCliqueBotaoMesAnterior() {

        botaoMesAnterior.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dateNow = DateHelpper.mesAnterior(dateNow);
                textviewMes.setText(DateHelpper.retornaNomeDoMes(dateNow));

            }
        });

    }

    public void defineCliqueBotaoMesPosterior() {
        botaoMesPosterior.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                dateNow = DateHelpper.mesPosterior(dateNow);
                textviewMes.setText(DateHelpper.retornaNomeDoMes(dateNow));

            }
        });
    }

}
